
<template>
  <div>
    <!--搜索框-->
    <el-form
      ref="schedulerFormRef"
      :model="schedulerForm"
      :inline="true"
      size="small"
    >
      <el-form-item label="创建人">
        <el-select v-model="schedulerForm.createId" placeholder="请选择创建人">
          <el-option
            v-for="item in userDropListOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="任务名称">
        <el-input v-model="schedulerForm.name" />
      </el-form-item>
      <el-form-item label="类型">
        <el-select
          v-model="schedulerForm.instanceType"
          placeholder="请选择类型"
        >
          <el-option
            v-for="item in schedulerForm.instanceTypeOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="状态">
        <el-select v-model="schedulerForm.status" placeholder="请选择状态">
          <el-option
            v-for="item in schedulerForm.statusOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="selectList">查询</el-button>

        <el-button type="primary" @click="add">新增</el-button>
      </el-form-item>
    </el-form>
    <!--表单-->
    <el-table
      v-loading="loading"
      :data="tableData"
      border
      style="width: 100%"
      size="small"
    >
      <!--表单展示--->
      <el-table-column prop="name" label="任务名称" >
        <template slot-scope="scope">
          <router-link   
            :to="{
              name: 'schedulerexecute',
              query: { id: scope.row.schedulerId },
            }"
          >
            <span style="color:#007bff">{{ scope.row.name }}</span></router-link
          >
        </template>
      </el-table-column>
      <el-table-column prop="cron" label="调度任务" />
      <el-table-column prop="jobName" label="job name" />
      <el-table-column prop="jobData" label="job data" />
      <el-table-column label="类型">
        <template slot-scope="scope">
          <el-tag type="success" v-if="scope.row.instanceType == 0"
            >单实例</el-tag
          >
          <el-tag type="success" v-if="scope.row.instanceType == 1"
            >多实例</el-tag
          >
        </template>
      </el-table-column>

      <el-table-column prop="instanceCount" label="实例数" />
      <el-table-column prop="executeCount" label="执行次数" />
      <el-table-column label="状态">
        <template slot-scope="scope">
          <el-tag type="success" v-if="scope.row.status == 0">运行</el-tag>
          <el-tag type="success" v-if="scope.row.status == 1">停止</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="createName" label="创建人" />
      <el-table-column prop="createTime" label="创建时间" />
      <!--表单操作-->
      <el-table-column label="操作">
        <template slot-scope="scope">
          <!--编辑-->
          <el-tooltip class="item" effect="dark" content="编辑" placement="top">
            <i
              class="el-icon-edit operationIcon"
              v-if="scope.row.schedulerType == 1"
              @click="edit(scope.row.schedulerId)"
            ></i>
          </el-tooltip>

          <!--立即运行-->
          <el-tooltip class="item" effect="dark" content="立即运行" placement="top">
            <i
              class="el-icon-caret-right operationIcon"
              v-if="scope.row.schedulerType == 1"
              @click="run(scope.row.schedulerId)"
            ></i>
          </el-tooltip>

          <!--删除-->
          <el-tooltip class="item" effect="dark" content="删除" placement="top">
            <i
              class="el-icon-delete operationIcon"
              v-if="scope.row.schedulerType == 1"
              @click="deleteRow(scope.row.schedulerId)"
            ></i>
          </el-tooltip>
        </template>
      </el-table-column>
    </el-table>
    <!--分页操作-->
    <el-pagination
      layout="total, sizes, prev, pager, next, jumper"
      :page-sizes="pageSizes"
      :total="total"
      :current-page.sync="pageNo"
      :page-size.sync="pageSize"
      @current-change="selectList"
      @size-change="selectList"
    />
    <!--新增任务调度弹窗-->
    <el-dialog
      title="新增任务调度"
      :visible.sync="addSchedulerDialog"
      :close-on-click-modal="false"
      width="600px"
    >
      <el-form
        ref="addSchedulerFormRef"
        :model="addSchedulerForm"
        :rules="addSchedulerFormRules"
        size="small"
        label-width="150px"
      >
        <el-form-item label="任务名称" prop="name">
          <el-input
            v-model="addSchedulerForm.name"
            type="name"
            placeholder="请填写任务名称"
          />
        </el-form-item>

        <el-form-item label="调度任务(cron)" prop="cron">
          <el-input
            v-model="addSchedulerForm.cron"
            type="cron"
            placeholder="请填写调度任务"
          />
        </el-form-item>
        <el-form-item label="job name" prop="jobName">
          <el-input
            v-model="addSchedulerForm.jobName"
            type="jobName"
            placeholder="请填写job name"
          />
        </el-form-item>
        <el-form-item label="job data" prop="jobData">
          <el-input
            v-model="addSchedulerForm.jobData"
            type="textarea"
            placeholder="请填写job data"
          />
        </el-form-item>

        <el-form-item label="类名" prop="className">
          <el-input
            v-model="addSchedulerForm.className"
            type="className"
            placeholder="请填写类名"
          />
        </el-form-item>

        <el-form-item label="类型">
          <el-select
            v-model="addSchedulerForm.instanceType"
            placeholder="请选择类型"
          >
            <el-option
              v-for="item in addSchedulerFormData.instanceTypeOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item
          label="实例数"
          prop="instanceCount"
          v-if="addSchedulerForm.instanceType == 1"
        >
          <el-input
            v-model="addSchedulerForm.instanceCount"
            type="instanceCount"
            placeholder="请填写实例数"
          />
        </el-form-item>

        <el-form-item label="状态">
          <el-select v-model="addSchedulerForm.status" placeholder="请选择状态">
            <el-option
              v-for="item in addSchedulerFormData.statusOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="备注" prop="remark">
          <el-input
            v-model="addSchedulerForm.remark"
            type="textarea"
            placeholder="请填写备注"
          />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button size="medium" @click="resetAddSchedulerForm()"
          >取消</el-button
        >
        <el-button
          v-loading="btnLoad"
          size="medium"
          type="primary"
          @click="addSchedulerSubmit"
          >提交</el-button
        >
      </span>
    </el-dialog>
    <!--编辑任务调度弹窗-->
    <el-dialog
      title="编辑任务调度"
      :visible.sync="editSchedulerDialog"
      :close-on-click-modal="false"
      width="600px"
    >
      <el-form
        ref="editSchedulerFormRef"
        :model="editSchedulerForm"
        :rules="editSchedulerFormRules"
        size="small"
        label-width="150px"
      >
        <el-form-item label="任务名称" prop="name">
          <el-input
            v-model="editSchedulerForm.name"
            type="name"
            placeholder="请填写任务名称"
         
          />
        </el-form-item>

        <el-form-item label="调度任务(cron)" prop="cron">
          <el-input
            v-model="editSchedulerForm.cron"
            type="cron"
            placeholder="请填写调度任务"
          />
        </el-form-item>

        <el-form-item label="类名" prop="className">
          <el-input
            v-model="editSchedulerForm.className"
            type="className"
            placeholder="请填写类名"
          />
        </el-form-item>

        <el-form-item label="类型">
          <el-select
            v-model="editSchedulerForm.instanceType"
            placeholder="请选择类型"
          >
            <el-option
              v-for="item in editSchedulerFormData.instanceTypeOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item
          label="实例数"
          prop="instanceCount"
          v-if="editSchedulerForm.instanceType == 1"
        >
          <el-input
            v-model="editSchedulerForm.instanceCount"
            type="instanceCount"
            placeholder="请填写实例数"
          />
        </el-form-item>

        <el-form-item label="状态">
          <el-select
            v-model="editSchedulerForm.status"
            placeholder="请选择状态"
          >
            <el-option
              v-for="item in editSchedulerFormData.statusOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="备注" prop="remark">
          <el-input
            v-model="editSchedulerForm.remark"
            type="textarea"
            placeholder="请填写备注"
          />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button size="medium" @click="resetEditSchedulerForm()"
          >取消</el-button
        >
        <el-button
          v-loading="btnLoad"
          size="medium"
          type="primary"
          @click="editSchedulerSubmit"
          >提交</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {
  addScheduler,
  schedulerList,
  viewScheduler,
  editScheduler,
  deleteScheduler,
  insertSchedulerSimple
} from "@/api/tool/dev/scheduler";
import { userDropList } from "@/api/drop";
import { Message } from "element-ui";

export default {
  data() {
    return {
      schedulerForm: {
        createId: "",
        name: "",
        instanceType: -1,
        instanceTypeOptions: [
          {
            value: -1,
            label: "请选择",
          },
          {
            value: 0,
            label: "单实例",
          },
          {
            value: 1,
            label: "多实例",
          },
        ],
        status: -1,
        statusOptions: [
          {
            value: -1,
            label: "请选择",
          },
          {
            value: 0,
            label: "运行",
          },
          {
            value: 1,
            label: "停止",
          },
        ],
        pageSize: 1,
        pageNum: 10,
      },
      userDropListOptions: [],
      tableData: [],
      //新增弹窗逻辑
      addSchedulerDialog: false,
      addSchedulerForm: {
        name: "",
        cron: "",
        className: "",
        instanceType: 0,
        instanceCount: "",
        jobName: "",
        jobData: "",
        status: 1,
        remark: "",
      },
      //添加弹窗数据源存放
      addSchedulerFormData: {
        instanceTypeOptions: [
          {
            value: 0,
            label: "单实例",
          },
          {
            value: 1,
            label: "多实例",
          },
        ],
        statusOptions: [
          {
            value: 1,
            label: "停止",
          },
          {
            value: 0,
            label: "运行",
          },
        ],
      },
      addSchedulerFormRules: {
        name: [{ required: true, trigger: "blur" }],
        cron: [{ required: true, trigger: "blur" }],
        jobName: [{ required: true, trigger: "blur" }],
        className: [{ required: true, trigger: "blur" }],
        instanceType: [{ required: true, trigger: "blur" }],
        instanceCount: [{ required: true, trigger: "blur" }],
        status: [{ required: true, trigger: "blur" }],
      },
      //编辑弹窗逻辑
      editSchedulerDialog: false,
      editSchedulerForm: {
        schedulerId: "",
        name: "",
        cron: "",
        className: "",
        instanceType: 0,
        instanceCount: "",
        status: 0,
        remark: "",
      },
      //编辑弹窗数据源存放
      editSchedulerFormData: {
        instanceTypeOptions: [
          {
            value: 0,
            label: "单实例",
          },
          {
            value: 1,
            label: "多实例",
          },
        ],
        statusOptions: [
          {
            value: 0,
            label: "运行",
          },
          {
            value: 1,
            label: "停止",
          },
        ],
      },
      editSchedulerFormRules: {
        name: [{ required: true, trigger: "blur" }],
        cron: [{ required: true, trigger: "blur" }],
        className: [{ required: true, trigger: "blur" }],
        instanceType: [{ required: true, trigger: "blur" }],
        instanceCount: [{ required: true, trigger: "blur" }],
        status: [{ required: true, trigger: "blur" }],
      },
      btnLoad: false,
      loading: false,
      pageSizes: [5, 10, 20, 30, 50],
      pageSize: 10,
      pageNo: 1,
      total: 0,
    };
  },
  mounted() {
    this.selectList();
    this.selectUserDropList();
  },
  methods: {
    //初始化角色信息
    selectUserDropList() {
      userDropList()
        .then((response) => {
          this.userDropListOptions = response.data.rows;
          this.loading = false;
        })
        .catch(() => {
          this.loading = false;
        });
    },
    // 初始化数据
    selectList() {
      this.loading = true;
      this.tableData = [];
      const params = {
        pageNum: this.pageNo,
        pageSize: this.pageSize,
        createId: this.schedulerForm.createId,
        name: this.schedulerForm.name,
        instanceType: this.schedulerForm.instanceType,
        status: this.schedulerForm.status,
      };
      schedulerList(params)
        .then((response) => {
          this.tableData = response.data.rows;
          this.total = response.data.total;
          this.loading = false;
        })
        .catch(() => {
          this.loading = false;
        });
    },
    //新增逻辑
    add() {
      this.addSchedulerDialog = true;
    },
    // 新增重置表单
    resetAddSchedulerForm() {
      this.addSchedulerForm = {
        name: "",
        cron: "",
        className: "",
        instanceType: 0,
        instanceCount: "",
        status: 0,
        remark: "",
      };
      this.addSchedulerDialog = false;
    },
    // 提交任务调度新增或编辑请求
    addSchedulerSubmit() {
      this.$refs.addSchedulerFormRef.validate((valid) => {
        if (valid) {
          this.btnLoad = true;
          // 新增任务调度
          addScheduler(this.addSchedulerForm)
            .then((response) => {
              this.btnLoad = false;
              Message.success(response.message);
              this.resetAddSchedulerForm();
              this.selectList();
            })
            .catch((res) => {
              this.btnLoad = false;
              this.resetAddSchedulerForm();
              Message.error(res.message);
            });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    //编辑逻辑
    edit(id) {
      this.editSchedulerDialog = true;
      viewScheduler(id)
        .then((response) => {
          this.editSchedulerForm = response.data;
        })
        .catch(() => {});
    },
    // 编辑重置表单
    resetEditSchedulerForm() {
      this.editSchedulerForm = {
        schedulerId: "",
        name: "",
        cron: "",
        className: "",
        instanceType: 0,
        instanceCount: "",
        status: 0,
        remark: "",
      };
      this.editSchedulerDialog = false;
    },
    // 提交任务调度编辑请求
    editSchedulerSubmit() {
      this.$refs.editSchedulerFormRef.validate((valid) => {
        if (valid) {
          this.btnLoad = true;
          // 新增任务调度
          editScheduler(this.editSchedulerForm)
            .then((response) => {
              this.btnLoad = false;
              Message.success(response.message);
              this.resetEditSchedulerForm();
              this.selectList();
            })
            .catch((res) => {
              this.btnLoad = false;
              this.resetEditSchedulerForm();
              Message.error(res.message);
            });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    // 删除
    deleteRow(id) {
      this.$confirm(`确定要删除该任务调度么？`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          deleteScheduler(id)
            .then((resp) => {
              Message.success(resp.message);
              this.selectList();
            })
            .catch((err) => {
            });
        })
        .catch(() => {});
    },
    run(id)
    {
      this.$confirm(`确定要立即运行此任务调度一次么？`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          var insertData={
            schedulerId:id
          };
          insertSchedulerSimple(insertData)
            .then((resp) => {
              Message.success(resp.message);
            })
            .catch((err) => {

            });
        })
        .catch(() => {});
    }
  },
};
</script>

<style lang="scss" scoped>
.operationIcon {
  margin-left: 10px;
}
.blueColor{
  color: blue;
}
</style>
